<template>
  <view class="content">
<image src="https://shenai-fducs.oss-cn-shanghai.aliyuncs.com/shenai_interface/welcome_bg.PNG" mode="aspectFill"
      class="bg_img" />
      <!--后面要放开-->
    <!--<button @tap="getCode" class ="testcode"> 显示code</button> 后面要删掉-->
    <!--<text class ="testcode" selectable>{{code}}</text>后面要删掉-->
  </view>
</template>



<script>
  import {
    wechat_login
  } from "@/api/login.js";
  export default {
    data() {
      return {
        code: 0//后面要删掉
      }
    },
    onLoad() {
      // this.getUserProfile();
      let shenai_userToken = wx.getStorageSync('shenai_userToken');
      console.log('shenai_userToken.token login');
      console.log(shenai_userToken.token);

      if (!shenai_userToken.token) {

        this.getChatCode();
        
      }else

      setTimeout(() => {   //后面要放开
        this.goInfo();
      }, 1000);
    },
    methods: {

      goInfo() {
        uni.switchTab({
          url: "/pages/home/home",
          animationType: 'pop-in',
          animationDuration: 200
        })
      },
      goUserDetail() {
        uni.navigateTo({
          url: "/subpkg-home/info/info",
        
        })
      },



      getChatCode() {
        console.log("*********getChatCode************")
        //因为作用域先提前赋值
        //使用uni封装的一键登录方法
        var that = this;
        uni.login({
          provider: 'weixin',
          success(res) {

            
            //成功后带着微信登录返回的code去请求我们的后端
            wechat_login({
              code: res.code
            }).then(res => {
              
              
              
              if (res.statusCode == 200)
                wx.setStorage({
                  key: 'shenai_userToken',

                  data: {
                    token: res.data.data.token
                  },
                  success: function() {
                     wx.setStorageSync('is_new', res.data.data.is_new);
                    if(res.data.data.is_new)
                    that.goUserDetail();  
                    else
                    that.goInfo()
                    console.log('shenai_userToken存储成功');
                  },
                  fail: function(error) {
                    console.log('shenai_userToken存储失败', error);
                  }
                });

            })
          }
        })
      },
      getCode(){
        var that = this
        uni.login({
          provider: 'weixin',
          success(res) {
            console.log("*********getweixinCode************")//后面要删掉
            console.log(res.code)//后面要删掉
            that.code = res.code//后面要删掉
            }
          })
      },
      
      getUserProfile() {
        var that = this
        uni.getUserProfile({
          desc: "获取你的昵称、头像",
          success(res) {
            console.log(res);
            // if (res.errMsg == "getUserProfile:ok" && res.userInfo != undefined) {
            //   //我用store来存储一些数据，你可以放到你要放的地方
            //   that.$store.state.userInfo.nickName = res.userInfo.nickName,
            //     that.$store.state.userInfo.avatarUrl = res.userInfo.avatarUrl
            //   that.getChatCode()
            // }
          },
          complete(res) {
            console.log(res);
          }
        })
      }




    }
  }
</script>



<style>
  .content {
    width: 100vw;
    height: 100vh;
  }

  .bg_img {
    width: 100vw;
    height: 100vh;
  }
  .testcode{
    width: 500rpx;
    height: 100rpx;
  }
</style>